﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：方超 
    //
    // 文件名：HospitalSelectAll.cs
    // 文件功能描述：多选医疗机构
    // 创建标识：方超2010-4-23
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Query;


namespace HZ.DrugTrade.Web.BidProdManage.DrugLibManage
{
    public partial class HospitalSelectAll : BasePage
    {
        
        public HospitalSelectAll()
        {
            this.ModuleNo = "";
        }

        IBLL_User iuser = Factory.BLL_User();
        IBLL_OperatingRule operateRuleCtrl = Factory.BLL_OperatingRule();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                StoreCondition();
                //此处调用目的，为了设置DropdownList的状态
                GetDefaultAreaString();
                //查询
                setBind();
            }
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            StoreCondition();
            setBind();
        }

        /// <summary>
        /// 定义绑定数据的方法
        /// </summary>
        private void setBind()
        {
            try
            {
                //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
                int totalRecord = 0;
                HZ.DrugTrade.Model.Query.HospitalCatalogManageRule pu = (HZ.DrugTrade.Model.Query.HospitalCatalogManageRule)ViewState["v_user"];
                IList<HZ.DrugTrade.Model.View.HospitalCatalogManageRule> mhrList = iuser.GetHospitalCatalogManageRulesByWhere(pu, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);

                //转换成DataTable
                this.gvwHospitalList.DataSource = mhrList;
                AspNetPager1.RecordCount = totalRecord;
                this.gvwHospitalList.DataBind();
            }
            catch (Exception ex)
            {
                PageError("数据连接失败！" + ex.Message, "");
            }
        }

        /// <summary>
        /// 存储查询条件
        /// </summary>
        private void StoreCondition()
        {
            HospitalCatalogManageRule aHospitalUsers = new HospitalCatalogManageRule();
            //入围权限ID
            aHospitalUsers.HospitalName = txtHospitalName.Text.Trim();
            aHospitalUsers.AreaID = GetAreaID();
            aHospitalUsers.SortFields = " HospitalID asc ";
            ViewState["v_user"] = aHospitalUsers;
        }

        /// <summary>
        /// 获取默认选中的地区编号串
        /// 供JS函数使用
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultProvince = "150000";
            string defaultCity = "";

            IBLL_Area areaCtrl = Factory.BLL_Area();
            Area area = areaCtrl.GetArea(CurrentAccount.GetUser().Org.Area.AreaID);

            //如果是市级中心机构，则默认选中具体城市
            if (area.FatherAreaID != "0")
            {
                defaultProvince = area.FatherAreaID;
                defaultCity = area.AreaID;
            }
            else
            {
                defaultProvince = area.AreaID;
            }

            string strArea = "";

            //显示默认地区
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;
            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ddlCountry"] + "\"";
            }


            return strArea;
        }

        /// <summary>
        /// 获取地区查询条件 AreaID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";

            //首次进入使用当前用户所在区域ID
            areaId = CurrentAccount.GetUser().Org.Area.AreaID;

            //优先获取区县，其次城市，再次省份
            if (new Common.ConvertHelper(Request.Form["ddlCountry"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ddlCountry"];
            }
            else
            {
                if (new Common.ConvertHelper(Request.Form["ddlCity"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ddlCity"];
                }
                else if (new Common.ConvertHelper(Request.Form["ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ddlProvice"];
                }

            }
            return areaId;
        }

        /// <summary>
        /// 批量选择医疗机构
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery0_Click(object sender, EventArgs e)
        {
            string strHosId = Request["chkHos"];
            if (!string.IsNullOrEmpty(strHosId))
            {
                string[] str = strHosId.Split(',');
                StringBuilder strID = new StringBuilder();
                StringBuilder strName = new StringBuilder();
                for (int i = 0; i < str.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        strID.Append(str[i] + "、");
                    }
                    else
                    {
                        strName.Append(str[i] + "、");
                    }
                }

                this.Page.RegisterStartupScript("", "<script>opener.insertHospitalInfo('" + strID.Remove(strID.Length - 1, 1).ToString() + "|" + strName.Remove(strName.Length - 1, 1).ToString() + "'); window.close();</script>");
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择医疗机构!')</script>");
            }
            
        }
    }
}
